import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 头部处理
  const { isBackShow } = to.meta
  store.commit('changeIsBackShow', { isBackShow })
  // 判断权限
  if (to.meta && to.meta.isPermission) {
    // console.log(store.state)
    if (store.state.user.token) {
      next()
    } else {
      next({ name: 'Login', params: { to } })
    }
  } else {
    next()
  }
})

export default router
